package org.bt.common.validator.notempty;

import org.bt.common.validator.aspect.NotEmpty;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Collection;

/**
 * Check that the collection is not null and not empty.
 *
 * @author tongw
 */
@SuppressWarnings("rawtypes")
public class NotEmptyValidatorForCollection implements ConstraintValidator<NotEmpty, Collection> {

    /**
     * Checks the collection is not {@code null} and not empty.
     *
     * @param collection                 the collection to validate
     * @param constraintValidatorContext context in which the constraint is evaluated
     * @return returns {@code true} if the collection is not {@code null} and the collection is not empty
     */
    @Override
    public boolean isValid(Collection collection, ConstraintValidatorContext constraintValidatorContext) {
        if (collection == null) {
            return false;
        }
        return collection.size() > 0;
    }
}
